package fr.web325.servlet.api.index;


import fr.web325.bean.Json;
import fr.web325.bean.User;
import fr.web325.dao.ClientDao;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/api/order")
public class Order extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if(session.getAttribute("user") == null){
            // 提示登录
            resp.getWriter().print(new Json(200,null,"请登录后再重试").getRespJson());
            return;
        }
        // 通过用户ID保存订单列表
        try {
            List<Map<String,Object>> orderList =  new ClientDao().getMineOrders(((User) session.getAttribute("user")).getId());
            JSONArray orderObject = JSONArray.fromObject(orderList);
            resp.getWriter().print(new Json(200,orderObject,"获取订单列表成功!").getRespJson());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
